package com.shadow.ui.events.event_container.base
{
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.utils.Dictionary;
	
	public class EventContainer extends EventDispatcher
	{
		protected var listeners:Dictionary;
		
		public function EventContainer()
		{
			this.listeners = new Dictionary();
			super();
		}
		
		/**
		 *事件侦听器此侦听器会将类型作为对象存到侦听列表内如果有相同的侦听器会移除侦听
		 * 
		 * @param type				事件类型
		 * @param listener			侦听回调方法
		 * @param useCapture		捕获阶段
		 * @param priority			事件优先级
		 * @param useWeakReference	是否使用弱引用
		 * 
		 */		
		override public function addEventListener(type:String, listener:Function, useCapture:Boolean=false, priority:int=0, useWeakReference:Boolean=true):void
		{
			var key:Object = {
				"type":type,
				"useCapture":useCapture
			}
			if (this.listeners[key])
			{
				removeEventListener(type, this.listeners[key], useCapture);
				this.listeners[key] = null;
				delete this.listeners[key];
			}
			this.listeners[key] = listener;
			super.addEventListener(type, listener, useCapture, priority, useWeakReference);
		}
		
		/**
		 *移除所有侦听器 
		 * 
		 */		
		protected function removeListeners():void
		{
			var key:Object;
			try{
				for (key in this.listeners)
				{
					removeEventListener(key.type, this.listeners[key], key.useCapture);
					listeners[key] = null;
					delete listeners[key];
				}
				
			}catch(error:Error)
			{
				trace("移除侦听器捕获");	
			}
		}
	}
}